package com.ljph.ljaker.validator;

import com.jfinal.kit.StrKit;
import com.ljph.jfinal.base.BaseValidator;
import com.ljph.ljaker.app.AppConfig;
import com.ljph.ljaker.model.User;

/**
 * Created by yuzhou on 16/9/18.
 */
public class UserSaveValidator extends BaseValidator{

    @Override
    protected void validate() {

        User user = new User();

        String username = controller.getPara("username");
        if(StrKit.isBlank(username)) {
            addError("用户名没有提供");
        }

        if(username.equalsIgnoreCase(AppConfig.SUPER)) {
            addError(username  + "为系统保留用户名");
        }

        if(User.dao.findByUsername(username) != null) {
            addError("用户" + username + "已经存在");
        }

        String password = controller.getPara("password");
        if(StrKit.isBlank(password)) {
            addError("密码没有没有提供");
        }

        user.setUsername(username);
        user.setPassword(password);

        String realName = controller.getPara("realName");
        if(StrKit.notBlank(realName)) user.setRealName(realName);

        String email = controller.getPara("email");
        if(StrKit.notBlank(email)) user.setEmail(email);

        String mobile = controller.getPara("mobile");
        if(StrKit.notBlank(mobile)) user.setMobile(mobile);

        Integer sex = controller.getParaToInt("sex");
        user.setSex(sex);

        controller.setAttr("user", user);
    }
}
